var rightwin = require('../../../template/rightwin/rightwin.js')
var {formatDate} = require('../../../utils/util')
var app = getApp()

var rightIcons = {
  "wallet-o": "money",
  "point": "dollar",
  "equity": "vip",
  "ticket": "coupon",
  "gift": "gift",
  "coffee": "cafe",
  "cake": "memory",
  "more": "more"
}

Page({
  data: {
    role: '0',
    rightsEnabled: 0
  },
  onLoad: function (options) {
    rightwin.init(this)
  },
  onShow: function () {
    app.getVipInfo().then(res => {
      this.updateInfo(res)
    })
  },
  updateInfo: function (vipInfo) {
    var rights = vipInfo.assets.LoyaltyCard[0].effects
    for (var i = 0; i < rights.length; i++) {
      if (rights[i].enabled === true) {
        rights[i].iconsrc = '../../imgs/my/rights/' + rightIcons[rights[i].icon] + '1.png'
      }
      else {
        rights[i].iconsrc = '../../imgs/my/rights/' + rightIcons[rights[i].icon] + '2.png'
      }
    }
    this.setData({
      role: vipInfo.userType,
      roleName: vipInfo.userTitle,
      bonusPoint: vipInfo.bonusPoint,
      rights: rights,
      rightsIntro: vipInfo.assets.LoyaltyCard[0].params.effectTitle
    })
    if (vipInfo.assets.LoyaltyCard[0].params.trySKU) {
      this.setData({
        cantry: true
      })
    }
    else {
      this.setData({
        cantry: false
      })
    }
    if (this.data.role === 'vip') {
      var endtime = formatDate(vipInfo.assets.LoyaltyCard[0].validEnd, 'yyyy-MM-dd')
      this.setData({
        endtime: endtime
      })
    }
  },
  dealVip: function () {
    app.showLoading()
    if (this.data.roleName === '金麦会员') {
      this.renewVip()
    }
    else {
      this.openVip()
    }
  },
  openVip: function () {
    var url = '/yuanmai_we/silo/card/create/' + app.globalData.vipInfo.assets.LoyaltyCard[0].params.buySKU
    app.request(url).then(res => {
      app.hideLoading()
      if (res.status === true) {
        wx.navigateTo({
          url: './buyvip/buyvip?payinfo=' + res.payInfo,
        })
      } else {
        app.toast.error(res.errMsg)
      }
    })
  },
  vipRightsDetail: function () {
    wx.navigateTo({
      url: './rightsdetail/rightsdetail',
    })
  },
  tryVip: function () {
    var url = '/silo/h5/2205.json'
    var params = {
      auth: app.globalData.authToken,
      dropKey: 'drop-trying-loyalty-card'
    }
    app.request(url, params).then(res => {
      if (res.protocError === 0) {
        app.getVipInfo(true).then(res => {
          this.updateInfo(res)
        })
      }
    })
  },
  renewVip: function () {
    var url = '/yuanmai_we/silo/card/extend/' + app.globalData.vipInfo.assets.LoyaltyCard[0].params.extendSKU + '/' + app.globalData.vipInfo.assets.LoyaltyCard[0].assetId
    app.request(url).then(res => {
      app.hideLoading()
      if (res.status === true) {
        wx.navigateTo({
          url: './buyvip/buyvip?payinfo=' + res.payInfo,
        })
      }
    })
  },
  closeRightWindow: rightwin.close,
  showRightwin: rightwin.show
})
